<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    use HasDateTimeFormatter;

    static public $identity = [
        1 => '个人',
        2 => '企业单位',
        3 => '政府机构',
    ];
    protected $appends = ['province_name_ch', 'city_name_ch', 'district_name_ch'];

    public function user()
    {
        return $this->belongsTo(AdminUser::class);
    }

    /**
     * 项目来源
     */
    public function prosource()
    {
        return $this->belongsTo('App\Models\ProSource', 'source', 'id');
    }

    public function province()
    {
        return $this->hasOne(ChinaAreas::class, 'id', 'province_name');
    }

    public function city()
    {
        return $this->hasOne(ChinaAreas::class, 'id', 'city_name');
    }

    public function district()
    {
        return $this->hasOne(ChinaAreas::class, 'id', 'district_name');
    }

    public function getProvinceNameChAttribute()
    {
        if (!isset($this->province)) {
            return null;
        }
        return $this->province->name;
    }

    public function getCityNameChAttribute()
    {
        if (!isset($this->city)) {
            return null;
        }
        return $this->city->name;
    }

    public function getDistrictNameChAttribute()
    {
        if (!isset($this->district)) {
            return null;
        }
        return $this->district->name;
    }
}
